// ImageHandler.h
//
// Copyright (c) 2002 Symbian Ltd.  All rights reserved.
//

#ifndef __IMAGEHANDLER_H__
#define __IMAGEHANDLER_H__

#include <e32base.h> 

#include "Connection.h"

class CFbsBitmap;
class CBitmapScaler;
class CImageDecoder;

//Callback from image resizer
class MImageHandlerObserver
{
public:
	virtual void ImageIsReady(TInt aError) = 0;
};

class CImageHandler :	public CActive,
						public MConnectionObserver
						
{
public:
	IMPORT_C static CImageHandler* NewLC(CFbsBitmap& aBitmap, MImageHandlerObserver& aObserver);
	IMPORT_C static CImageHandler* NewL(CFbsBitmap& aBitmap, MImageHandlerObserver& aObserver);
	IMPORT_C ~CImageHandler();

private: //constructor and destructors
	CImageHandler(CFbsBitmap& aBitmap, MImageHandlerObserver& aObserver);
	void ConstructL();

private: //from CActive
	void RunL(); 
	void DoCancel();
	TInt RunError(TInt aError);


private: //from MConnectionObserver
	void ResponseRequestedL(TDesC8& aResponse);
	void StreamingRequestedL(TDesC8& aChunk) {}
	void StreamingEndedL() {}
	void ResponseErrorL(TInt aError);


public: //new functions
	IMPORT_C void GetAndConvertL(const TDesC& aImageUrl, const TSize& aSize);


private:
	void ConvertL(TDesC8& aImageData);
	void ScaleL();
	void SetRequestStatus(TInt aError);
	const TDesC8& GetMimeType();


private: //data
	enum EImageHandlerState
	{
		ENotInitiated,
		EConverting,
		EScaling,
		EImageReady,
		EFailed
	} iState;

	//Observer
	MImageHandlerObserver& iObserver;

	//Reference to bitmap
	CFbsBitmap& iBitmap;

	//Reference to connection object
	CConnection* iConnection;

	//Bitmap scaler
	CBitmapScaler* iScaler;

	//Bitmap decoder
	CImageDecoder* iDecoder;
	HBufC* iFileExtension;

	//Size to scale
	TSize iSize;
};

#endif	// __IMAGEHANDLER_H__